package com.learn.micro_mall.service.api.goods.impl;

import com.learn.micro_mall.common.response.PlainResult;
import com.learn.micro_mall.common.util.PlainResultUtils;
import com.learn.micro_mall.dao.skuParam.model.SkuParam;
import com.learn.micro_mall.domain.entity.category.CategoryEntity;
import com.learn.micro_mall.domain.entity.goods.GoodsSkuParamEntity;
import com.learn.micro_mall.domain.service.goods.GoodsSkuParamValidatorDomainService;
import com.learn.micro_mall.domain.vo.goods.GoodsSkuParamId;
import com.learn.micro_mall.repository.goods.GoodsSkuParamRepository;
import com.learn.micro_mall.service.api.goods.GoodsSkuParamService;
import com.learn.micro_mall.service.convertor.goods.GoodsSkuParamMapper;
import com.learn.micro_mall.service.request.goods.GoodsSkuParamCreateRequest;
import com.learn.micro_mall.service.request.goods.GoodsSkuParamDeleteRequest;
import com.learn.micro_mall.service.request.goods.GoodsSkuParamUpdateRequest;
import com.learn.micro_mall.service.response.goods.GoodsSkuParamCreateResponse;
import com.learn.micro_mall.service.response.goods.GoodsSkuParamDeleteResponse;
import com.learn.micro_mall.service.response.goods.GoodsSkuParamUpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goodsSkuParam")
public class GoodsSkuParamServiceImpl implements GoodsSkuParamService {

    @Autowired
    private GoodsSkuParamValidatorDomainService goodsSkuParamValidatorDomainService;

    @Resource
    private GoodsSkuParamRepository goodsSkuParamRepository;

    @Override
    @RequestMapping("/create")
    @Transactional(rollbackFor = Exception.class)
    public PlainResult<GoodsSkuParamCreateResponse> createGoodsSkuParam(GoodsSkuParamCreateRequest request) throws Exception {
        GoodsSkuParamId goodsSkuParamId = GoodsSkuParamId.builder().skuParamKey(request.getSkuParamKey()).build();
        goodsSkuParamValidatorDomainService.validatorSkuParamNoExist(goodsSkuParamId);
        GoodsSkuParamEntity goodsSkuParamEntity = new GoodsSkuParamEntity();
        goodsSkuParamEntity.setGoodsSkuParamId(GoodsSkuParamId.builder().skuParamKey(request.getSkuParamKey()).build());
        for (String skuParamValue : request.getSkuParamValues()) {
            goodsSkuParamEntity.setSkuParamValue(skuParamValue);
            goodsSkuParamRepository.insert(goodsSkuParamEntity);
        }
        GoodsSkuParamCreateResponse response = new GoodsSkuParamCreateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }

    @Override
    @RequestMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public PlainResult<GoodsSkuParamUpdateResponse> updateGoodsSkuParam(GoodsSkuParamUpdateRequest request) throws Exception {
        // 在更新商品参数时,需要验证是否有商品关联了该商品参数
        GoodsSkuParamId goodsSkuParamId = GoodsSkuParamId.builder().skuParamKey(request.getSkuParamKey()).build();
        goodsSkuParamValidatorDomainService.validatorSkuParamExist(goodsSkuParamId);
        // todo 这里的业务规则是: 商品和参数相关联,关联数就新增1,所以要删除的话,就必须该商品和属性没有关联才能删除
        for (String skuParamValue : request.getDeletedSkuParamValues()) {
            goodsSkuParamRepository.delete(goodsSkuParamId.getSkuParamKey(), skuParamValue);
        }
        GoodsSkuParamEntity goodsSkuParamEntity = new GoodsSkuParamEntity();
        goodsSkuParamEntity.setGoodsSkuParamId(goodsSkuParamId);
        for (String skuParamValue : request.getNewSkuParamValues()) {
            goodsSkuParamEntity.setSkuParamValue(skuParamValue);
            goodsSkuParamRepository.insert(goodsSkuParamEntity);
        }
        GoodsSkuParamUpdateResponse response = new GoodsSkuParamUpdateResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }

    @Override
    public PlainResult<GoodsSkuParamDeleteResponse> deleteGoodsSkuParam(GoodsSkuParamDeleteRequest request) throws Exception {
        List<SkuParam> skuParamList = goodsSkuParamRepository.listByGoodsSkuParamId(request.getSkuParamKey());
        List<SkuParam> skuParamsRelatedGoods = skuParamList.stream().filter(skuParam -> skuParam.getRelatedGoodsNum() != 0).collect(Collectors.toList());
        if(!skuParamsRelatedGoods.isEmpty()){
            throw new Exception("该商品参数不能删除");
        }
        //todo 这里同样有一个问题,在删除商品参数时新增了一个关联,此时要如何解决该问题呢
        goodsSkuParamRepository.delete(request.getSkuParamKey());
        //        goodsSkuParamRepository.delete(goodsSkuParamId);
        GoodsSkuParamDeleteResponse response = new GoodsSkuParamDeleteResponse();
        response.setSuccess(true);
        return PlainResultUtils.success(response);
    }


}
